package com.itheima.canal;

import com.itheima.dao.MoneyLogDao;
import com.itheima.domain.MoneyLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import java.util.List;

/*****
 * @Author: http://www.itheima.com
 * @Description: com.itheima.canal.MoneyLogAsync
 ****/
//@Component
//@CanalTable(value = "money_log") //监听哪张表变更
public class MoneyLogAsync  implements EntryHandler<MoneyLog>{

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private MoneyLogDao moneyLogDao;

    @Autowired
    private RestTemplate restTemplate;

    /****
     * 增加--抢红包
     */
    @Override
    public void insert(MoneyLog moneyLog) {
       //将数据同步到Redis[用户抢红包，更新用户红包记录]
        //查询数据
        List<MoneyLog> moneyLogs = moneyLogDao.list(moneyLog.getUsername());
        redisTemplate.boundHashOps("UserMoneyLog").put(moneyLog.getUsername(),moneyLogs);

        try {
            //将数据同步到Nginx缓存[抢红包排行榜更新]  http://192.168.211.141/purge/mlog/top
            //http://192.168.211.141/mlog/top
            // http://192.168.211.141/purge/mlog/top
            String url ="http://192.168.211.141/purge/mlog/top";
            restTemplate.getForObject(url,String.class);
        } catch (RestClientException e) {
        }
    }


    /***
     * 指定表发生删除数据的时候，触发执行
     * @param moneyLog
     */
    @Override
    public void delete(MoneyLog moneyLog) {
        System.out.println("删除数据："+moneyLog);
    }
}
